package com.shell.governor.user.rx;

import com.shell.bee.base.exception.UnCheckMsgException;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.governor.user.UserEvent;
import com.shell.governor.user.po.PubUser;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

@Component
public class PostUserToEsignSubscriber extends OnErrorNotImplementedSubscriber<UserEvent> {
	@Value("${esign.user.serviceApp}")
	private String serviceApp;
	@Autowired
	RestTemplate restTemplate;
	@Autowired
	private LoadBalancerClient loadBalancerClient;

	public void onApplicationEvent(UserEvent event) {
		PubUser user = event.getUser();
		Map<String, String> ret = new HashMap<String, String>();
		if ("on".equals(SpringContextUtil.getProperty("esign.switch"))) {
			ret = postUserToEsign(user);
		} else if ("on".equals(SpringContextUtil.getProperty("cfca.switch"))) {
			ret = postUserToCfca(user);
		}
		if ((ret != null) && (ret.size() > 0) && (!"0".equals(ret.get("errorCode")))) {
			throw new UnCheckMsgException("��������������" + (String) ret.get("msg"));
		}
	}

	private Map<String, String> postUserToEsign(PubUser user) {
		if (StringUtil.empty(this.serviceApp)) {
			this.logger.info("��������������������������������������");
			return null;
		}
		String[] serviceList = this.serviceApp.split(",");

		Map<String, String> data = new HashMap<String, String>();

		MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
		map.add("userId", user.getUserId());
		map.add("userName", user.getUserName());
		map.add("phoneNo", user.getPhoneNo());
		map.add("idNo", user.getCertNo());

		ServiceInstance serviceInstance = null;
		for (String serviceName : serviceList) {
			serviceInstance = this.loadBalancerClient.choose(serviceName);
			if (serviceInstance == null) {
				this.logger.error("��������������������������������" + serviceName);
			} else {
				this.logger.info("��������������--------->" + serviceInstance.getUri().toString() + "/" + serviceName
						+ "/saveEsignCust");

				Map<String, String> msgData = (Map) this.restTemplate.postForObject(
						serviceInstance.getUri().toString() + "/" + serviceName + "/saveEsignCust", map, Map.class,
						new Object[0]);
				if (null != msgData) {
					data.putAll(msgData);
				}
			}
		}
		return data;
	}

	private Map<String, String> postUserToCfca(PubUser user) {
		if (StringUtil.empty(this.serviceApp)) {
			this.logger.info("��������������������������������������");
			return null;
		}
		String[] serviceList = this.serviceApp.split(",");

		Map<String, String> data = new HashMap();

		MultiValueMap<String, Object> map = new LinkedMultiValueMap();
		map.add("userName", user.getUserName());
		map.add("phoneNo", user.getPhoneNo());
		map.add("certType", user.getCertType());
		map.add("idNo", user.getCertNo());

		ServiceInstance serviceInstance = null;
		for (String serviceName : serviceList) {
			serviceInstance = this.loadBalancerClient.choose(serviceName);
			if (serviceInstance == null) {
				this.logger.error("��������������������������������" + serviceName);
			} else {
				this.logger.info("��������������--------->" + serviceInstance.getUri().toString() + "/" + serviceName
						+ "/authCfcauserAcct");

				Map<String, String> msgData = (Map) this.restTemplate.postForObject(
						serviceInstance.getUri().toString() + "/" + serviceName + "/authCfcauserAcct", map, Map.class,
						new Object[0]);
				if (null != msgData) {
					data.putAll(msgData);
				}
			}
		}
		return data;
	}
}
